package com.xxl.job.consume.kafka;

import com.xxl.job.consume.common.KafkaGroupConstant;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: kafka消费者配置
 */

@Configuration
@EnableKafka
public class KafkaConsumeConfig {
    @Value("${kafka.consumer.servers}")
    private String servers;
    @Value("${kafka.consumer.enable.auto.commit}")
    private boolean enableAutoCommit;
    @Value("${kafka.consumer.session.timeout}")
    private String sessionTimeout;
    @Value("${kafka.consumer.auto.commit.interval}")
    private String autoCommitInterval;
    @Value("${kafka.consumer.auto.offset.reset}")
    private String autoOffsetReset;
    @Value("${kafka.consumer.concurrency}")
    private int concurrency;
    @Value("${kafka.consumer.max.poll.records}")
    private int maxPollRecords;


    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> hkListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(hkConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> hkConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.HK_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }

    @Bean
    public KafkaConsumer rqUdpConsumerListener() {
        return new KafkaConsumer();
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> rqListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(rqConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> rqConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.RQ_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> dhListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(dhConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> dhConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.DH_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }

//
//    @Bean
//    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> blListenerContainerFactory() {
//        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
//        factory.setConsumerFactory(blConsumerFactory());
//        factory.setConcurrency(concurrency);
//        factory.setBatchListener(true);
//        factory.getContainerProperties().setPollTimeout(4000);
//        return factory;
//    }
//
//    public ConsumerFactory<String, String> blConsumerFactory() {
//        Map<String, Object> properties = consumerConfigs();
//        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.BL_GROUP);
//        return new DefaultKafkaConsumerFactory<String, String>(properties);
//    }
//
    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> xfListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(xfConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> xfConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.XF_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> apListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(apConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> apConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.AP_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> gatherListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(gatherConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(4000);
        return factory;
    }

    public ConsumerFactory<String, String> gatherConsumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, KafkaGroupConstant.GATHER_ALARM_GROUP);
        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }


    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        propsMap.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);
        return propsMap;
    }




}
